package com.haois.mp.wechat.job;

import com.haois.mp.database.module.common.daomain.RemindDob;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.haois.mp.wechat.task.BirthdayRemindTask;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on hgits.cn
 * @date 2018/10/30 10:05
 */
@Component
@JobHandler(value = "birthdayRemindJobHandler")
public class BirthdayRemindJobHandler extends IJobHandler {
    @Autowired
    BirthdayRemindTask birthdayRemindTask;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        Map<String, Object> params = null;
        if ("day".equals(param)) {
            params = birthdayRemindTask.remindBirthdayThisDay();
        } else if ("week".equals(param)) {
            params = birthdayRemindTask.remindBirthdayThisWeek();
        } else if ("month".equals(param)) {
            params = birthdayRemindTask.remindBirthdayThisMonth();
        } else if ("year".equals(param)) {
            params = birthdayRemindTask.updateOnBirthday();
        }
        String msg = "";
        try {
            String accordSize = String.valueOf(params.get("accordSize"));
            String pushSize = String.valueOf(params.get("pushSize"));
            List<RemindDob> remindDobList = (List<RemindDob>) params.get("content");
            if (remindDobList != null) {
                remindDobList.forEach(remindDob -> {
                    XxlJobLogger.log(remindDob.toString());
                });
            }
            msg = "本次任务中符合条件数据共" + accordSize + "条，合计推送" + pushSize + "次";
        } catch (Exception e) {
            msg = "任务可能已经完成，但是日志记录失败";
        }
        SUCCESS.setMsg(msg);
        return SUCCESS;
    }
}
